//
//  DTViewController.m
//  us
//
//  Created by karry on 2025/3/29.
//

#import "DTViewController.h"
#import <FLAnimatedImage/FLAnimatedImage.h>

@interface DTViewController ()

@end

@implementation DTViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor jk_colorWithHex:0xEFEFE8];
    self.displayEmptySet = YES;
    
}

- (UITableView *)us_tableView{
    if (!_us_tableView) {
        _us_tableView = [self us_defaultTableView];
        _us_tableView.emptyDataSetSource = self;
        _us_tableView.emptyDataSetDelegate = self;
    }
    return _us_tableView;
}

- (UIActivityIndicatorView *)indicator{
    if (!_indicator) {
        _indicator = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleMedium];
    }
    return _indicator;
}

- (UIBarButtonItem *)doneItem{
    if (!_doneItem) {
        _doneItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"ic_gouzi"] style:UIBarButtonItemStylePlain target:self action:@selector(doneAction:)];
    }
    return _doneItem;
}

- (void)doneAction:(id)sender{}


#pragma mark - Public API

- (void)startLoading {
    self.dataState = EmptyDataStateLoading;
    [self.us_tableView reloadEmptyDataSet];
}

- (void)loadingSuccess {
    self.dataState = EmptyDataStateNone;
    [self.us_tableView reloadData];
}

- (void)loadingFailed {
    self.dataState = EmptyDataStateError;
    [self.us_tableView reloadEmptyDataSet];
}

- (void)loadingNoData {
    self.dataState = EmptyDataStateNoData;
    [self.us_tableView reloadEmptyDataSet];
}

#pragma mark - DZNEmptyDataSet

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    if (!self.displayEmptySet) {
        return nil;
    }
    
    switch (self.dataState) {
        case EmptyDataStateLoading:
            return nil;
        case EmptyDataStateError:
            return [UIImage systemImageNamed:@"exclamationmark.warninglight"];
        case EmptyDataStateNoData:
            return [UIImage imageNamed:@"empty_icon"];
        default:
            return nil;
    }
}

- (UIView *)customViewForEmptyDataSet:(UIScrollView *)scrollView {
    
    if (!self.displayEmptySet) {
        return nil;
    }
    
    if (self.dataState == EmptyDataStateLoading) {
        UIView *contentView = [UIView new];
        contentView.frame = CGRectMake(0, 0, self.view.jk_width, 200);
        
        NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"load_page" ofType:@"gif"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:nil];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.frame = CGRectMake(0, 0, 200, 200);
        [imageView sd_setImageWithURL:[NSURL fileURLWithPath:gifPath]];
        
        [contentView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.height.mas_equalTo(200);
            make.center.mas_equalTo(contentView);
        }];
        
        return contentView;
    }
    return nil;

}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView{
    if (!self.displayEmptySet) {
        return 0;
    }
    return -kNavigationBarHeight;
}


- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    if (!self.displayEmptySet) {
        return nil;
    }
    NSString *text;
    switch (self.dataState) {
        case EmptyDataStateLoading:
            text = @"加载中…";
            break;
        case EmptyDataStateError:
            text = @"加载失败，点击重试";
            break;
        case EmptyDataStateNoData:
            text = @"暂无数据";
            break;
        default:
            text = @"";
            break;
    }
    
    return [[NSAttributedString alloc] initWithString:text attributes:@{
        NSFontAttributeName: [UIFont systemFontOfSize:15],
        NSForegroundColorAttributeName: [UIColor grayColor]
    }];
}

- (BOOL)emptyDataSetShouldFadeIn:(UIScrollView *)scrollView{
    return YES;
}


- (BOOL)emptyDataSetShouldAllowTouch:(UIScrollView *)scrollView {
    if (!self.displayEmptySet) {
        return NO;
    }
    return self.dataState != EmptyDataStateLoading;
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    
    if (!self.displayEmptySet) {
        return;
    }
    
    if (self.dataState == EmptyDataStateError) {
        [self loadData];
    }
}

- (void)loadData{
    
}

@end
